home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter16 / isohex16_1 / isotilewalker.cpp < prev    next >
C/C++ Source or Header  |  2000-07-26  |  5KB  |  250 lines

  1. //IsoTileWalker.cpp
  2. #include "IsoTileWalker.h"
  3.  
  4. //prototypes for tilewalker functions
  5. POINT IsoHex_SlideTileWalk(POINT ptStart,ISODIRECTION IsoDirection);
  6. POINT IsoHex_StagTileWalk(POINT ptStart,ISODIRECTION IsoDirection);
  7. POINT IsoHex_DiamondTileWalk(POINT ptStart,ISODIRECTION IsoDirection);
  8. POINT IsoHex_RectTileWalk(POINT ptStart,ISODIRECTION IsoDirection);
  9.  
  10. //constructor
  11. CTileWalker::CTileWalker()
  12. {
  13.     SetMapType(ISOMAP_RECTANGULAR);
  14. }
  15.  
  16. //destructor
  17. CTileWalker::~CTileWalker()
  18. {
  19. }
  20.  
  21. //set map type
  22. void CTileWalker::SetMapType(ISOMAPTYPE IsoMapType)
  23. {
  24.     //store map type
  25.     this->IsoMapType=IsoMapType;
  26.  
  27.     //set the tilewalker function
  28.     switch(IsoMapType)
  29.     {
  30.     case ISOMAP_SLIDE:
  31.         {
  32.             IsoHexTileWalkerFn=IsoHex_SlideTileWalk;
  33.         }break;
  34.     case ISOMAP_STAGGERED:
  35.         {
  36.             IsoHexTileWalkerFn=IsoHex_StagTileWalk;
  37.         }break;
  38.     case ISOMAP_DIAMOND:
  39.         {
  40.             IsoHexTileWalkerFn=IsoHex_DiamondTileWalk;
  41.         }break;
  42.     case ISOMAP_RECTANGULAR:
  43.         {
  44.             IsoHexTileWalkerFn=IsoHex_RectTileWalk;
  45.         }break;
  46.     }
  47. }
  48.  
  49. //get map type
  50. ISOMAPTYPE CTileWalker::GetMapType()
  51. {
  52.     return(IsoMapType);
  53. }
  54.  
  55. //tile walking
  56. POINT CTileWalker::TileWalk(POINT ptStart,ISODIRECTION IsoDirection)
  57. {
  58.     return(IsoHexTileWalkerFn(ptStart,IsoDirection));
  59. }
  60.  
  61. //tile walker functions
  62. //slide
  63. POINT IsoHex_SlideTileWalk(POINT ptStart,ISODIRECTION IsoDirection)
  64. {
  65.     //move ptStart depending on direction
  66.     switch(IsoDirection)
  67.     {
  68.     case ISO_NORTH://move north
  69.         {
  70.             ptStart.y-=2;
  71.             ptStart.x++;
  72.         }break;
  73.     case ISO_NORTHEAST://move northeast
  74.         {
  75.             ptStart.x++;
  76.             ptStart.y--;
  77.         }break;
  78.     case ISO_EAST://move east
  79.         {
  80.             ptStart.x++;
  81.         }break;
  82.     case ISO_SOUTHEAST://move southeast
  83.         {
  84.             ptStart.y++;
  85.         }break;
  86.     case ISO_SOUTH://move south
  87.         {
  88.             ptStart.y+=2;
  89.             ptStart.x--;
  90.         }break;
  91.     case ISO_SOUTHWEST://move southwest
  92.         {
  93.             ptStart.x--;
  94.             ptStart.y++;
  95.         }break;
  96.     case ISO_WEST://move west
  97.         {
  98.             ptStart.x--;
  99.         }break;
  100.     case ISO_NORTHWEST://move northwest
  101.         {
  102.             ptStart.y--;
  103.         }break;
  104.     }
  105.     //return the location moved to
  106.     return(ptStart);
  107. }
  108.  
  109. //staggered
  110. POINT IsoHex_StagTileWalk(POINT ptStart,ISODIRECTION IsoDirection)
  111. {
  112.     //move ptStart depending on direction
  113.     switch(IsoDirection)
  114.     {
  115.     case ISO_NORTH://move north
  116.         {
  117.             ptStart.y-=2;
  118.         }break;
  119.     case ISO_NORTHEAST://move northeast
  120.         {
  121.             ptStart.x+=((ptStart.y&1));
  122.             ptStart.y--;
  123.         }break;
  124.     case ISO_EAST://move east
  125.         {
  126.             ptStart.x++;
  127.         }break;
  128.     case ISO_SOUTHEAST://move southeast
  129.         {
  130.             ptStart.x+=((ptStart.y&1));
  131.             ptStart.y++;
  132.         }break;
  133.     case ISO_SOUTH://move south
  134.         {
  135.             ptStart.y+=2;
  136.         }break;
  137.     case ISO_SOUTHWEST://move southwest
  138.         {
  139.             ptStart.x+=((ptStart.y&1)-1);
  140.             ptStart.y++;
  141.         }break;
  142.     case ISO_WEST://move west
  143.         {
  144.             ptStart.x--;
  145.         }break;
  146.     case ISO_NORTHWEST://move northwest
  147.         {
  148.             ptStart.x+=((ptStart.y&1)-1);
  149.             ptStart.y--;
  150.         }break;
  151.     }
  152.     //return the location moved to
  153.     return(ptStart);
  154. }
  155.  
  156. //diamond
  157. POINT IsoHex_DiamondTileWalk(POINT ptStart,ISODIRECTION IsoDirection)
  158. {
  159.     //move ptStart depending on direction
  160.     switch(IsoDirection)
  161.     {
  162.     case ISO_NORTH://move north
  163.         {
  164.             ptStart.x--;
  165.             ptStart.y--;
  166.         }break;
  167.     case ISO_NORTHEAST://move northeast
  168.         {
  169.             ptStart.y--;
  170.         }break;
  171.     case ISO_EAST://move east
  172.         {
  173.             ptStart.x++;
  174.             ptStart.y--;
  175.         }break;
  176.     case ISO_SOUTHEAST://move southeast
  177.         {
  178.             ptStart.x++;
  179.         }break;
  180.     case ISO_SOUTH://move south
  181.         {
  182.             ptStart.x++;
  183.             ptStart.y++;
  184.         }break;
  185.     case ISO_SOUTHWEST://move southwest
  186.         {
  187.             ptStart.y++;
  188.         }break;
  189.     case ISO_WEST://move west
  190.         {
  191.             ptStart.x--;
  192.             ptStart.y++;
  193.         }break;
  194.     case ISO_NORTHWEST://move northwest
  195.         {
  196.             ptStart.x--;
  197.         }break;
  198.     }
  199.     //return the location moved to
  200.     return(ptStart);
  201. }
  202.  
  203. //rectangular
  204. POINT IsoHex_RectTileWalk(POINT ptStart,ISODIRECTION IsoDirection)
  205. {
  206.     //move ptStart depending on direction
  207.     switch(IsoDirection)
  208.     {
  209.     case ISO_NORTH://move north
  210.         {
  211.             ptStart.y--;
  212.         }break;
  213.     case ISO_NORTHEAST://move northeast
  214.         {
  215.             ptStart.y--;
  216.             ptStart.x++;
  217.         }break;
  218.     case ISO_EAST://move east
  219.         {
  220.             ptStart.x++;
  221.         }break;
  222.     case ISO_SOUTHEAST://move southeast
  223.         {
  224.             ptStart.x++;
  225.             ptStart.y++;
  226.         }break;
  227.     case ISO_SOUTH://move south
  228.         {
  229.             ptStart.y++;
  230.         }break;
  231.     case ISO_SOUTHWEST://move southwest
  232.         {
  233.             ptStart.x--;
  234.             ptStart.y++;
  235.         }break;
  236.     case ISO_WEST://move west
  237.         {
  238.             ptStart.x--;
  239.         }break;
  240.     case ISO_NORTHWEST://move northwest
  241.         {
  242.             ptStart.x--;
  243.             ptStart.y--;
  244.         }break;
  245.     }
  246.     //return the location moved to
  247.     return(ptStart);
  248. }
  249.  
  250.